home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / GREPSTUF / GREPGREP.C < prev    next >
Text File  |  1986-10-31  |  4KB  |  241 lines

  1. /*
  2.     GrepGrep.c - Grep and Word Count functions
  3. */
  4.  
  5.  
  6. # include    "Grep.h"
  7.  
  8. static long    lineNum;
  9.  
  10.  
  11. /*
  12.     Set the grep state.  Val is either 0 (false) or 255 (true).
  13. */
  14.  
  15. GrepState (val)
  16. int        val;
  17. {
  18.     HiliteControl (pauseCtl, val);
  19.     HiliteControl (cancelCtl, val);
  20.     grepping = ! (Boolean) val;
  21. }
  22.  
  23.  
  24. /*
  25.     Display lines matching (or not matching) pattern.  This is
  26.     called to get a line at a time.  Mouse clicks control the
  27.     state of the pause variable.
  28. */
  29.  
  30. GrepLine ()
  31. {
  32. char    buf[bufSiz];
  33.  
  34.     if (!paused)
  35.     {
  36.         if (StreamGetS (buf) == nil)
  37.         {
  38.             StopGrep ();
  39.         }
  40.         else
  41.         {
  42.             ++lineNum;
  43.             PtoCstr (buf);
  44.             if (match (buf) == prtMatches)
  45.             {
  46.                 if (prtLineNum)
  47.                 {
  48.                     DisplayLong (lineNum);
  49.                     DisplayString ("\p: ");
  50.                 }
  51.                 CtoPstr (buf);
  52.                 DisplayString (buf);
  53.                 DisplayLn ();
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59.  
  60. /*
  61.     Cancel any current grep operation.
  62. */
  63.  
  64. StopGrep ()
  65. {
  66.     if (grepping)
  67.     {
  68.         CloseStream ();
  69.         GrepState (255);
  70.     }
  71. }
  72.  
  73.  
  74. /*
  75.     Start grepping a file
  76. */
  77.  
  78. StartGrep ()
  79. {
  80.  
  81.     StopGrep ();        /* terminate any ongoing grep operation */
  82.     if (!havePat)
  83.         GetGrepPat ();
  84.     if (/* now */ havePat)
  85.     {
  86.         if (GetStream ())    /* do grep setup */
  87.         {
  88.             lineNum = 0;
  89.             paused = false;
  90.             SetCTitle (pauseCtl, "\pPause");
  91.             GrepState (0); /* turn controls on, grepping true */
  92.         }
  93.     }
  94. }
  95.  
  96.  
  97. /*
  98.     Word Count
  99. */
  100.  
  101.  
  102. Wc ()
  103. {
  104. register long    lines = 0;
  105. long    nonEmptyLines = 0;
  106. long    words = 0;
  107. register long    chars = 0;
  108. register Boolean    inToken = false;
  109. register int    c, lastc;
  110.  
  111.     for (lastc = '\r'; /* empty */ ; lastc = c)
  112.     {
  113.         c = StreamGetC ();
  114.         if (c == -1)
  115.             break; /* eof */
  116.         ++chars;
  117.         switch (c)
  118.         {
  119.             case '\r':
  120.             {
  121.                 ++lines;
  122.                 if (lastc != '\r')
  123.                     ++nonEmptyLines;
  124.                 inToken = false;
  125.                 break;
  126.             }
  127.             case ' ':
  128.             case '\t':
  129.             {
  130.                 inToken = false;
  131.                 break;
  132.             }
  133.             default:
  134.             {
  135.                 if (inToken == false)
  136.                 {
  137.                     ++words;
  138.                     inToken = true;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.  
  144.     if (lastc != '\r')    /* in case of missing cr on last line */
  145.     {
  146.         ++lines;
  147.         ++nonEmptyLines;
  148.     }
  149.     DisplayLong (chars);
  150.     DisplayString ("\p Chars, ");
  151.     DisplayLong (words);
  152.     DisplayString ("\p Words, ");
  153.     DisplayLong (lines);
  154.     if (streamType == text)
  155.         DisplayString ("\p Lines\r");
  156.     else
  157.     {
  158.         DisplayString ("\p Paragraphs (");
  159.         DisplayLong (nonEmptyLines);
  160.         DisplayString ("\p non-empty)\r");
  161.     }
  162. }
  163.  
  164.  
  165. /*
  166.     Add string to display area.  First insert it at the end.  Test if
  167.     must scroll lines off top to get the new stuff to show up.  If yes,
  168.     then do the scroll.  To keep from filling up the TERec, delete
  169.     whatever got scrolled out of view every once in a while.  (The number
  170.     of lines scrolled off the top to check for is arbitrary - I clobber
  171.     stuff after every 25 lines.)  To avoid unnecessary redrawing, set to
  172.     no clip before doing the delete (which would redraw) and the scroll
  173.     back down (which would also redraw).
  174.  
  175.     Also write string to output file, if one is open.
  176. */
  177.  
  178. DisplayString (theStr)
  179. StringPtr    theStr;
  180. {
  181. register int    dispLines;     /* number of lines displayable in window */
  182. register int    topLines;     /* number of lines currently scrolled off top */
  183. register int    scrollLines; /* number of lines to scroll up */
  184. register int    height;
  185. long            len;
  186. Rect            r;
  187.  
  188.     len = theStr[0];
  189.     height = (**teHand).lineHeight;
  190.     TESetSelect (32760L, 32760L, teHand); /* set to insert at end */
  191.     TEInsert (theStr+1, len, teHand);
  192.     r = (**teHand).viewRect;
  193.     dispLines = (r.bottom - r.top) / height;
  194.     topLines = (r.top - (**teHand).destRect.top) / height;
  195.     scrollLines = (**teHand).nLines - topLines - dispLines;
  196.     if (scrollLines > 0) /* must scroll up */
  197.     {
  198.         TEScroll (0, -height * scrollLines, teHand); /* scroll up */
  199.         topLines += scrollLines;
  200.         if (topLines > 25)    /* keep TERec from filling up */
  201.         {
  202. /*
  203.     clobber first line(s), and scroll back down to resync what will then
  204.     be the first line.  Set clipping empty, so that the redraw from the
  205.     delete and the scroll down are not shown.
  206. */
  207.             SetRect (&r, 0, 0, 0, 0);
  208.             ClipRect (&r);
  209.             TESetSelect (0L, (**teHand).lineStarts[topLines], teHand);
  210.             TEDelete (teHand);
  211.             TEScroll (0, height * topLines, teHand);
  212.             ClipRect (&theWind->portRect);
  213.         }
  214.     }
  215.  
  216.     if (fileOpen)
  217.     {
  218.         if (FSWrite (outFile, &len, theStr+1) != noErr)
  219.         {
  220.             Alarm ("\pWrite Error (Closing File)");
  221.             FileOutput ();
  222.         }
  223.     }
  224. }
  225.  
  226.  
  227. DisplayLn ()
  228. {
  229.     DisplayString ("\p\r");
  230. }
  231.  
  232.  
  233. DisplayLong (l)
  234. long        l;
  235. {
  236. Str255        s;
  237.  
  238.     NumToString (l, s);
  239.     DisplayString (s);
  240. }
  241.